const app = getApp();

Component({
  properties: {
    attrs: {
      type: Array,
      value: ''
    },
    skus: {
      type: Array,
      value: []
    },
  },
  data: {
    hideAttr: true,
    list : [],
    attrHistory:[]
  },

  ready: function () {
  },
  methods: {
    init:function(edit = false,show = true){
      this.data.edit = edit;
      if(show){
        this.setData({
          hideAttr : false
        })
      }
      //如果attr前后两次attr值是不相等的，也就是规格值有改变，需要重新计算可能性
      var eq = this.attrEq();
      if(this.data.list.length == 0 || !eq){
        this.data.attrHistory = this.data.attrs;
        if (!this.data.attrs || this.data.attrs.length == 0){
          return ;
        }
        var attrs = this.data.attrs;
        var arrays = [];
        for (var i = 0; i < attrs.length;i++){
          if(attrs[i].value && attrs[i].value.length > 0){
            arrays.push(attrs[i].value);
          }
        }
        var list = this.getArrayByArrays(arrays);
        var newList = [];
        //重构可能性格式
        var skuLen = this.data.skus.length;
        if(skuLen > 0 && edit){
          var sku_array = [];
          //得到编辑前每种组合的字符串并关联库存和价格
          this.data.skus.forEach((item,index)=>{
            var str = '';
            var sku_values = JSON.parse(item.sku_values);
            for ( let val of Object.values(sku_values)){
              str = str + '-' + val
            }
            str = str.substr(1);
            sku_array.push({value:str,price:item.sale_price,stock:item.stock});
          })
          //如果新数组某项组合字符串等于编辑前组合字符串，则认为两者是同一可能性，将原价格和库存复制给新的数组
          list.forEach((item) => {
            item = item.substr(1);  //去掉第一个字符
            sku_array.forEach((sku) => {
              if(sku.value == item){
                newList.push({
                  value:item,
                  stock:sku.stock,
                  price:sku.price
                })
              }
            });
          });
        }else{
          //非编辑情况下直接将库存和价格设为固定值
          list.forEach((item) => {
            if(item){
              item = item.substr(1);  //去掉第一个字符
              newList.push({
                value:item,
                stock:10,
                price:0
              })
            }
          });
        }
        
        this.setData({
          list: newList
        })
        if(edit || !show){
          this.save();
        }
      }
      
    },
    //判断两个数组是否相等
    attrEq :function() {
      if(this.data.attrs.toString() == this.data.attrHistory.toString()){
        var len = this.data.attrs.length;
        for(var i = 0;i< len; i++){
          if(this.data.attrs[i].value.toString() != this.data.attrHistory[i].value.toString() ){
            return false;
          }
        }
        return true
      }
      return false;
    },
    /*获取二维数组的所有可能 */
    getArrayByArrays:function (arrays)
    {
      var arr = [""];
      for(var i = 0; i<arrays.length;i++)
      {
        arr = this.getValuesByArray(arr, arrays[i]);
      } 
      
      return arr;
     
    },
    getValuesByArray:function (arr1, arr2) {
      var arr = [];
      for (var i = 0; i < arr1.length; i++) {
        var v1 = arr1[i];
        for (var j = 0; j < arr2.length; j++) {
          var v2 = arr2[j];
          var value = v1 + '-' + v2;
          arr.push(value);
        };
      };
      return arr;
    },
    //
    inputStock:function(e){
      var index = e.currentTarget.dataset.index;
      var val =  e.detail.value;
      this.data.list[index].stock = val;
    },
    inputPrice:function(e){
      var index = e.currentTarget.dataset.index;
      var val =  e.detail.value;
      this.data.list[index].price = val;
    },
    //上一步
    last:function(){
      this.setData({
        hideAttr: true
      })
    },
    save:function(){
      this.triggerEvent('getPrice', { list: this.data.list });
      this.setData({
        hideAttr: true
      })
    }
  }
})